//
// Created by gogoqiu on 2021/8/24.
//

#include "InputDialog.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(wxtestDialog)
#include <wx/settings.h>
#include <wx/string.h>
#include <wx/intl.h>
#include <wx/font.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(wxtestDialog)
const long InputDialog::ID_TEXT_TITLE = wxNewId();
const long InputDialog::ID_TEXT_VALUE = wxNewId();
const long InputDialog::ID_BUTTON_OK = wxNewId();
const long InputDialog::ID_BUTTON_CANCEL = wxNewId();
const long InputDialog::ID_LISTBOX1 = wxNewId();
const long InputDialog::ID_TEXTCTRL1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(InputDialog,wxDialog)
                //(*EventTable(wxtestDialog)
                //*)
END_EVENT_TABLE()

/*
 * 如何交互的
 *
 */
InputDialog::InputDialog(wxWindow* parent,PreferenceView *preferenceView, wxWindowID id)
{
    this->preferenceView = preferenceView;
    //(*Initialize(wxtestDialog)
    Create(parent, id, _("输入"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));
    buildView();

    Connect(ID_BUTTON_OK,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&InputDialog::OnOk);
    Connect(ID_BUTTON_CANCEL,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&InputDialog::OnCancel);
    Connect(wxID_ANY,wxEVT_INIT_DIALOG,(wxObjectEventFunction)&InputDialog::OnInit);
    //*)
}

InputDialog::~InputDialog()
{
    //(*Destroy(wxtestDialog)
    //*)
}

/*
 * title
 *  input
 *      cancel  ok
 * baseSizer
 *  label
 *      input
 *          BoxSizer2
 *              button button
 * 将panel将所有内容都封装起来
 */
void InputDialog::buildView(){
    //this->preferenceView;
    SetClientSize(wxSize(400,200));
    m_panel = new wxPanel( this );
    m_panel->SetBackgroundColour( wxColour( 0, 128, 192 ) );
    baseSizer = new wxBoxSizer(wxVERTICAL);

    baseSizer->Add( m_panel, 1,
                    wxEXPAND|wxALL, 10);
    //vector<PreferenceView*>::iterator _iterator;
    //idx ++;
    //wxSizerSpacer
    //titleCtrl = new wxStaticText( m_panel,  wxID_ANY, _iterator.operator*()->getTitle(), wxDefaultPosition, wxDefaultSize, 0 );
    wxBoxSizer *wBox = new wxBoxSizer( wxVERTICAL );
    titleCtrl = new wxStaticText( m_panel,  ID_TEXT_TITLE,
                                  this->preferenceView->getTitle(),
                                wxDefaultPosition, wxDefaultSize, 0 );
    wBox->Add( titleCtrl, 1,
                    wxALL|wxALIGN_LEFT, 10);
    //current value
    valueCtrl = new wxTextCtrl( m_panel,  ID_TEXT_VALUE,
                                this->preferenceView->getValue(),
                                wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
    wBox->Add( valueCtrl, 1,
               wxEXPAND|wxALL, 10);

    BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
    //baseSizer->Add(BoxSizer2, 0,
     //              wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4);
    ok = new wxButton( this, ID_BUTTON_OK, "OK");
    cancel = new wxButton( this, ID_BUTTON_CANCEL, "CANCEL");

    BoxSizer2->Add( ok, wxALL|wxALIGN_CENTER_HORIZONTAL, 4);

    BoxSizer2->Add( cancel, wxALL|wxALIGN_CENTER_HORIZONTAL, 4);
    wBox->Add( BoxSizer2, 0,
               wxALIGN_RIGHT, 4 );
    //SetSizer(baseSizer);
    //SetSizer(BoxSizer1);
    //Layout();
    ok->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler( InputDialog::OnOk ),
                     NULL, this );
    cancel->Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler( InputDialog::OnCancel ),
                    NULL, this );
    m_panel->SetSizer( wBox );
    m_panel->Layout();
    SetSizer( baseSizer );
    //SetSizer(BoxSizer1);
    Layout();
}

void InputDialog::OnQuit(wxCommandEvent& event)
{
    Close();
}

void InputDialog::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void InputDialog::OnInit(wxInitDialogEvent& event)
{
}

//post value to parent
void InputDialog::OnOk(wxMouseEvent& event)
{
    if ( !valueCtrl->GetValue().empty() )
        preferenceView->setValue(valueCtrl->GetValue());
    this->AcceptAndClose();
}

void InputDialog::OnCancel(wxMouseEvent& event)
{
    this->Close();
}